﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.Jidong
{
    public class 设备编码提示 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.CacheControl = "no-cache";
            context.Response.Expires = -1;

            string s = context.Request["s"];
            if (s == null) return;
            s = s.Replace("'", "");
            s = s.Trim();
            if (s == "" || s.Length < 2 || s == "000000") return;
            
            string sql = "select 资产,rtrim(编码) as 编码,名称 from 设备编码 where";

            if (!s.StartsWith("000000"))
            {
                sql += " 资产=0 and (编码='" + s.Substring(0, 2) + "'";
                if (s.Length >= 4 && s.Substring(2, 2) != "00") sql = sql + " or 编码='" + s.Substring(0, 4) + "'";
                if (s.Length >= 6 && s.Substring(4, 2) != "00") sql = sql + " or 编码='" + s.Substring(0, 6) + "'";
                sql += ")";
            }
            

            if (s.Length >= 8)
            {
                if (!sql.EndsWith("where")) sql += " or";
                string s2 = s.Substring(6);
                sql = sql + " 资产=1 and (编码='" + s2.Substring(0, 2) + "'";
                if (s2.Length >= 4 && s2.Substring(2, 2) != "00") sql = sql + " or 编码='" + s2.Substring(0, 4) + "'";
                if (s2.Length >= 6 && s2.Substring(4, 2) != "00") sql = sql + " or 编码='" + s2.Substring(0, 6) + "'";
                if (s2.Length >= 8 && s2.Substring(6, 2) != "00") sql = sql + " or 编码='" + s2.Substring(0, 8) + "'";
                sql += ")";
            }

            if (s.Length >= 14)
            {
                sql += " union select * from (select top 3 2 as 资产,编码,名称 from 设备台账 where 编码 like '" + s.Substring(0, 14) + "%' order by 编码 desc) t1";
            }
            sql += " order by 资产 asc,编码 asc";
            //context.Response.Write(sql);
            //context.Response.End();



            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;

            
            Cmd.CommandText = sql;

            SqlDataReader Reader = Cmd.ExecuteReader();
            int r=0;
            while (Reader.Read())
            {
                context.Response.Write(Reader[1] + ":" + Reader[2] +"\n");
                r++;
            }
            Reader.Close();
            Conn.Close();
            Reader.Dispose();
            Cmd.Dispose();
            Conn.Dispose();
 
            if (r == 0)
            {
                context.Response.Write("编码规则不存在");
            }
            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
